package com.longteng.diamond.controller;

import com.longteng.diamond.domain.InterfaceScript;
import com.longteng.diamond.domain.ReportLog;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.service.ScriptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Administrator
 * DATE : 2018/8/12
 * TIME : 15:44
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Controller
@RequestMapping("/scriptController")
@Slf4j
public class ScriptController {
    @Autowired
    ScriptService scriptService;

    /**
     * 脚本编写页面
     * @param interfaceAliasName
     * @param interfaceScript
     * @return ModelAndView
     */
    @RequestMapping("/scriptPage")
    public ModelAndView scriptPage(InterfaceScript interfaceScript, String interfaceAliasName) {
        ModelAndView modelAndView = new ModelAndView();
        /**用interfaceMockId去获取当前这个接口的脚本,如果为空的时间，从模版里面取脚本**/
        InterfaceScript dbInterfaceScript = scriptService.getScript(interfaceScript.getInterfaceMockId());
        if (null == dbInterfaceScript) {
            interfaceScript.setScript(this.getScriptModel());
        } else {
            BeanUtils.copyProperties(dbInterfaceScript, interfaceScript);
        }
        modelAndView.setViewName("/page/script/scriptPage");
        modelAndView.addObject("interfaceScript", interfaceScript);
        modelAndView.addObject("interfaceAliasName", interfaceAliasName);
        return modelAndView;
    }
    /**
     * 更新脚本
     * @param interfaceScript
     * @return RequestResult
     */
    @RequestMapping("/updateScript")
    public @ResponseBody
    RequestResult updateScript(InterfaceScript interfaceScript) {
        RequestResult requestResult = new RequestResult();
        try {
            String script = interfaceScript.getScript();
            script=script.replace("\"","@@");
            interfaceScript.setScript(script);
            scriptService.updateScript(interfaceScript);
            requestResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            requestResult.setSuccess(false);
            requestResult.setMsg("更新脚本异常,请联系管理员!");
        }
        return requestResult;
    }
    /**
     * 读取调试日志
     * @param fileName
     * @param lastTimeFileSize
     * @return ReportLog
     */
    @RequestMapping("/debugLog")
    public @ResponseBody
    ReportLog debugLog(String fileName, Long lastTimeFileSize){
        return  scriptService.debugLog(fileName,lastTimeFileSize);
    }
    /**
     * 修改脚本的是否调试状态
     * @param interfaceScript
     * @return ReportLog
     */
    @RequestMapping("/updateDebugStatus")
    public @ResponseBody
    RequestResult updateDebugStatus(InterfaceScript interfaceScript){
        RequestResult requestResult=new RequestResult();
        try {
            scriptService.updateDebugStatus(interfaceScript);
            requestResult.setSuccess(true);
        }catch (Exception e){
            log.error("更新脚本调试状态异常",e);
            requestResult.setSuccess(false);
            requestResult.setMsg("更新脚本调试状态异常,请联系管理员!");
        }
        return requestResult;
    }
    /**
     * 清除日志
     * @param fileName
     * @return RequestResult
     */
    @RequestMapping("/clearLog")
    public @ResponseBody RequestResult clearLog(String fileName){
        RequestResult requestResult=new RequestResult();
        try {
            scriptService.clearLog(fileName);
            requestResult.setSuccess(true);
        }catch (Exception e){
            log.error("清除日志异常",e);
            requestResult.setSuccess(false);
            requestResult.setMsg("清除调试日志异常,请联系管理员!");
        }
        return requestResult;
    }
    /**
     * 自定义脚本模版
     * @return String
     */
    private String getScriptModel() {
        return "import java.util.*;\n" +
                "import com.alibaba.fastjson.JSON;\n" +
                "import javax.servlet.http.HttpServletRequest;\n" +
                "/**\n" +
                "*自定义脚本模版,以下是6个内置变量,可以直接使用\n" +
                "*@param url 调用者的url路径，可以直接使用例子: println(url);\n" +
                "*@param 请求方法:println(interfaceMethod)\n" +
                "*@param 请求入参,url上面带的key 和value,这个是map类型,例子 paramMap.get(@@xxx@@);\n" +
                "*@param 请求入参body,post请求的入参body例子: Map map=JSON.parseObject(body,Map.class);\n" +
                "*@param 请求消息头,例子: String contentType=headerMap.get(@@content-type@@);\n" +
                "*@param httpRequest,具体用法百度一下，" +
                "*从请求url的参数获取name的值,例子:String paramValue= httpRequest.getParameter(@@name@@);\n" +
                "*@return 返回mock数据的id\n" +
                "*/\n" +
                "/**\n" +
                "*调试:如果你想要调试,查看获取的值使用: println(name); 输出变量\n" +
                "*例子: \n" +
                "*get的url参数请求\n" +
                "*http://www.baidu.com?id=2&name=xiaoqiang\n" +
                "*def id = paramMap.get(@@id@@);\n" +
                "*def name = paramMap.get(@@name@@);\n" +
                "*post的body请求\n" +
                "*可以从body里面拿，可以转换成对象 Map map=JSON.parseObject(body,Map.class);\n" +
                "*如果匹配到了,返回你想要的mock数据的id,如果没有任何匹配返回0,认为没有数据匹配\n" +
                "*/\n" +
                "return 0;";
    }
}